package com.proxy.scanandcheck.checker.controller;

import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import com.proxy.scanandcheck.checker.dao.ProxyDao;
import com.proxy.scanandcheck.checker.thread.ThreadPool;
import com.proxy.scanandcheck.checker.thread.ThreadPoolTask;
import com.proxy.scanandcheck.common.valueobject.Proxy;

/**
 * Proxy checking follow control.
 *
 */
@Service
public class ProxyCheckingController {
	
	
	@Autowired
	private ThreadPoolTaskExecutor checkingThreadPoolTaskExecutor;
	
	private HttpClient httpclient = null; 
	
	public ProxyCheckingController()
	{
		initial();
	}
	
	private void initial()
	{
		// Create an HttpClient with the ThreadSafeClientConnManager.
        // This connection manager must be used if more than one thread will
        // be using the HttpClient.
        PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
        connectionManager.setMaxTotal(50);
        connectionManager.setDefaultMaxPerRoute(5);
        
        httpclient = new DefaultHttpClient(connectionManager);
	}
	
	/**
	 * Get proxy data from proxyDao object, start threadPool object.
	 * @throws SQLException
	 * @throws FileNotFoundException 
	 */
	public List<Document> startCheck(List<String> words) throws SQLException, FileNotFoundException
	{
		ThreadPool threadPool = new ThreadPool();
		ProxyDao proxyDao = new ProxyDao();
		List<Proxy> myProxyList = proxyDao.getProxyList();
		
		// Create an HttpClient with the ThreadSafeClientConnManager.
        // This connection manager must be used if more than one thread will
        // be using the HttpClient.
        PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
        connectionManager.setMaxTotal(50);
        connectionManager.setDefaultMaxPerRoute(5);
        
        HttpClient httpclient = new DefaultHttpClient(connectionManager);
		
        threadPool.start(httpclient,myProxyList,words);
        
        List<Document> documentList = new ArrayList<Document>();
        for(Proxy proxy : myProxyList)
        {
        	if(proxy.getDocument() != null)
        	{
        		documentList.add(proxy.getDocument());
        	}
        }
		
		httpclient.getConnectionManager().shutdown();
		
		
		return documentList;
	}
	
	public void startCheck(List<Proxy> myProxyList, List<String> words) 
	{
		
		for (String word : words) {
			for(Proxy myProxy : myProxyList)
			{
				try {
					checkingThreadPoolTaskExecutor.execute(new ThreadPoolTask(httpclient,myProxy,word));
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
        
	}
	
	public void stopCheck()
	{
		httpclient.getConnectionManager().shutdown();
	}

}
